<?php

     session_start();
     include('common.php');
     include('../includes/pager.php');

    if (session_is_registered(MySBB_Admin_username) and $member_permission == 1)
    {

// **  **

         if ($_GET['add'] == 1)
         {
             if ($_GET['index'] == 1)
             {
                 $SF->Make_cplink_path("<a href='faq.php?main=1'>المساعدة</a> -> إضافة سؤال جديد");
                 $Smarty->display('faq_add.tpl');
             }

             if ($_GET['start'] == 1)
             {
                 if (empty($_POST['f1']) or empty($_POST['f2']) or empty($_POST['f3']) or empty($_POST['f4']))
                 {
                     $SF->error('يرجى تعبئة كافة المعلومات');
                 }

                 $insert = $DB->sql_query("INSERT INTO " . $db_prefix . "faq(id,title,faq_order,faq_describe,text) VALUES('NULL','" . $_POST['f1'] . "','" . $_POST['f3'] . "','" . $_POST['f2'] . "','" . $_POST['f4'] . "')");

              

                 if ($insert)
                 {
                     $SF->msg('تم الاضافة بنجاح !');
                     $SF->go_to('faq.php?main=1',2);
                 }
             }
         }

// **  **

         if ($_GET['main'] == 1)
         {
             if (!isset($_GET['page']) OR $_GET['page'] < 1)
             {
                 $_GET['page'] = 1;
             }

             $page  = intval($_GET['page']);
             $start = ($info_row['perpage'] * ($page-1));

             $RP = new Pager($page);

             $faq_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "faq ORDER BY faq_order ASC LIMIT 0,99");

             $RP->SetPagerN($info_row['perpage'],$DB->sql_num_rows($DB->sql_query("SELECT * FROM " . $db_prefix . "faq  ORDER BY id ASC LIMIT 0," . $info_row['perpage'] ."")));

             $faq_row = array();
             while ($faq_row2 = $DB->sql_fetch_array($faq_query))
             {
                 $faq_row[] = $faq_row2;
                 $Smarty->assign('faq_row',$faq_row);
             }

             $SF->Make_cplink_path('نظام الامساعدة');
             print $RP->PageNum('main=1');
             $Smarty->display('faq_main.tpl');
         }

// **  **

         if ($_GET['edit'] == 1)
         {
             if ($_GET['index'] == 1)
             {
                 $getfaq_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "faq WHERE id='" . intval($_GET['id']) . "'");
                 $getfaq_row   = $DB->sql_fetch_array($getfaq_query);

                 $Smarty->assign_by_ref('getfaq_row',$getfaq_row);

                 $SF->Make_cplink_path("<a href='smile.php?main=1'>نظام المساعدة</a> -> تحرير سؤال");
                 $Smarty->display('faq_edit.tpl');
             }

             if ($_GET['start'] == 1)
             {
                  if (empty($_POST['f1']) or empty($_POST['f2']) or empty($_POST['f3']) or empty($_POST['f4']))
                 {
                     $SF->error('يرجى تعبئة كافة المعلومات');
                 }

               $update = $DB->sql_query("UPDATE " . $db_prefix . "faq SET faq_order='" . $_POST['f3'] . "' , title='" . $_POST['f1'] . "' ,  text='" . $_POST['f4'] . "' , faq_describe='" . $_POST['f2'] . "'  WHERE id='" . intval($_GET['id']) . "'");
       
                 if ($update)
                 {
                     $SF->msg('تم التحديث بنجاح !');
                     $SF->go_to('faq.php?main=1',2);
                 }
             }
         }

// **  **

         if ($_GET['del'] == 1)
         {
             $del = $DB->sql_query("DELETE FROM " . $db_prefix . "faq WHERE id='" . intval($_GET['id']) . "'");

             if ($del)
             {
                 $SF->msg('تم الحذف بنجاح !');
                 $SF->go_to('faq.php?main=1',2);
             }
         }

// **  **

    }

?>